<?php
if(!defined('SYSTEM_PATH' ) ) die('Cannot access this page directly or see your configuration file ' ) ;
/**
* Error
* 
* Gestion des log
* Gestion des erreurs
* 
* @package cosmoClan
* @author menTiss - Staff SeS 
* @copyright 2007-2008
* @version 0.1.0
* @access public 
*/
class Error{
    protected $_error = array() ;
    protected $_logPath ;

    public function __construct()
    {
        if(defined('PATH_LOG' ) && is_writable(PATH_LOG ) ){
            $this->_logPath = PATH_LOG ;
        }else{
            die('cosmoPHP cannot be initialized, if you are administrator of the site please view your configuration' ) ;
        }
    }
    /**
    * Cosmo::addLog()
    * 
    * @desc 	Ajoute au fichier de logging en fonction du type de log (debug, error, notice...etc. whatever you want)
    * 
    * @version 1.1
    * @history	1.0 -- menTiss -- Added verification system to only log what user want (5 level, see configuration file for more informations)
    * @todo	menTiss : maybe use syslog or integrated system to manage log system
    * 
    * @param 	string $type 
    * @param 	string $msg 
    * @return 	bool
    */
    final public function addLog($type, $msg )
    {
        switch(LOG_LEVEL){
            case 0 :
                $log = array() ;
                break ;
            case 1 :
                $log = array('error' ) ;
                break ;
            case 2 :
                $log = array('error', 'debug' ) ;
                break ;
            case 3 :
                $log = array('error', 'debug', 'info' ) ;
                break ;
            case 4 :
                $log = array('error', 'info' ) ;
                break ;
        }
        if(in_array(strtolower($type ), $log ) ){
            $today = date('Ydm' ) ;
            if($log = fopen($this->_logPath . '/' . $today . '_' . strtolower($type) . '.log', 'a' ) ){
                fwrite($log, date('H:i:s' ) . ' : ' . $msg . "\n" ) ;
                fclose($log ) ;
                return true ;
            }else{
                return false;
            }
        }
    }
    /**
    * Cosmo::isError()
    * 
    * @return bool
    */
    final public function isError()
    {
        return isset($this->_error['message'] ) ;
    }
    /**
    * Cosmo::setError()
    * 
    * @param string $msg 
    * @param boolean $stop 
    * @return void
    */
    final public function setError($msg, $stop = false )
    {
        $this->_error['message'] = $msg ;
        $this->_error['stop'] = $stop ;
    }
    /**
    * Cosmo::getError()
    * 
    * @return string
    */
    final public function getError()
    {
        $this->addLog('error', $this->_error['message'] ) ;
        return $this->_error['message'] ;
    }
    /**
    * Cosmo::isStopped()
    * 
    * @return bool
    */
    final public function isStopped()
    {
        return $this->_error['stop'] ;
    }
    /**
     * Error::sendError()
     * 
     * @desc 		Send error to the system
     * 
     * @author 	menTiss
     * 
     * @date 		01/11/2007
     * @version 	1.0
     * 
     * @param 		string $msg
     * @param 		string $level
     * 
     * @return 	void
     */
    final public function sendError($msg, $level = E_USER_NOTICE) {
    		$levelCheck = array(E_USER_NOTICE,E_USER_WARNING,E_USER_ERROR) ;
    		$errorLevel = explode('|', ERROR_LEVEL) ;
		switch($level) {
			default :
			case E_USER_NOTICE 	:
				$systemError = E_NOTICE ;
				break ;
			case E_USER_WARNING :
				$systemError = E_WARNING ;
				break ;
			case E_USER_ERROR 	:
				$systemError = E_ERROR ;				
				break ;   
		}
		if(in_array($level, $levelCheck) && ($errorLevel == E_ALL || in_array($systemError, $errorLevel))) 
			trigger_error((string)$msg, $level);
    }
}

?>